{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "udacity-cs344-hw5",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/depctg/udacity-cs344-colab/blob/master/notebook/udacity_cs344_hw5.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "metadata": {
        "id": "hse6gSyUS5ka",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "# Homework 5 for Udacity CS344 Course, Intro to Parallel Programming\n",
        "# clone the code repo,\n",
        "!git clone https://github.com/depctg/udacity-cs344-colab\n",
        "!pip install git+git://github.com/depctg/nvcc4jupyter.git\n",
        "\n",
        "# load cuda plugin\n",
        "%config NVCCPluginV2.static_dir = True\n",
        "%config NVCCPluginV2.relative_dir = \"udacity-cs344-colab/src/HW5\"\n",
        "%load_ext nvcc_plugin\n",
        "\n",
        "# change to work directory, generate makefiles\n",
        "!mkdir udacity-cs344-colab/build\n",
        "%cd udacity-cs344-colab/build\n",
        "!cmake ../src"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "3vA0JP15TORh",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "%%cuda --name student.cu\n",
        "\n",
        "/* Udacity HW5\n",
        "   Histogramming for Speed\n",
        "\n",
        "   The goal of this assignment is compute a histogram\n",
        "   as fast as possible.  We have simplified the problem as much as\n",
        "   possible to allow you to focus solely on the histogramming algorithm.\n",
        "\n",
        "   The input values that you need to histogram are already the exact\n",
        "   bins that need to be updated.  This is unlike in HW3 where you needed\n",
        "   to compute the range of the data and then do:\n",
        "   bin = (val - valMin) / valRange to determine the bin.\n",
        "\n",
        "   Here the bin is just:\n",
        "   bin = val\n",
        "\n",
        "   so the serial histogram calculation looks like:\n",
        "   for (i = 0; i < numElems; ++i)\n",
        "     histo[val[i]]++;\n",
        "\n",
        "   That's it!  Your job is to make it run as fast as possible!\n",
        "\n",
        "   The values are normally distributed - you may take\n",
        "   advantage of this fact in your implementation.\n",
        "\n",
        "*/\n",
        "\n",
        "\n",
        "#include \"utils.h\"\n",
        "\n",
        "__global__\n",
        "void yourHisto(const unsigned int* const vals, //INPUT\n",
        "               unsigned int* const histo,      //OUPUT\n",
        "               int numVals)\n",
        "{\n",
        "  //TODO fill in this kernel to calculate the histogram\n",
        "  //as quickly as possible\n",
        "\n",
        "  //Although we provide only one kernel skeleton,\n",
        "  //feel free to use more if it will help you\n",
        "  //write faster code\n",
        "}\n",
        "\n",
        "void computeHistogram(const unsigned int* const d_vals, //INPUT\n",
        "                      unsigned int* const d_histo,      //OUTPUT\n",
        "                      const unsigned int numBins,\n",
        "                      const unsigned int numElems)\n",
        "{\n",
        "  //TODO Launch the yourHisto kernel\n",
        "\n",
        "  //if you want to use/launch more than one kernel,\n",
        "  //feel free\n",
        "\n",
        "  cudaDeviceSynchronize(); checkCudaErrors(cudaGetLastError());\n",
        "}"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "sSAnpiE2nL1T",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "# make the cuda project\n",
        "!make HW5\n",
        "print(\"\\n====== RESULT OF HW5 =======\\n\")\n",
        "!bin/HW5"
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}